package com.zzz.schedule.filter;

import com.zzz.schedule.pojo.SysUser;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

/*
 * @author zzr
 * @date: 2025/07/28  22:35
 * @description: 登录的过滤器
 */
//@WebFilter(urlPatterns = {"/showSchedule.html", "/schedule/*"})
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 参数父转子
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        // 获得 session 对象
        HttpSession session = request.getSession();
        // 从 session 中获得登录的用户对象
        SysUser sysUser = (SysUser)session.getAttribute("sysUser");
        // 判断对象是否为空
        if (null == sysUser) {
            // 没登陆 跳转到登录页面
            response.sendRedirect("/login.html");
        } else {
            // 已经登录过 放行
            filterChain.doFilter(request, response);
        }
    }
}
